import lesson
import pygtk
pygtk.require('2.0')
import gtk
import lessonmode
import os

TYPING_ROOT = os.path.dirname(os.path.abspath(__file__))
while(os.path.isfile(TYPING_ROOT)):
    TYPING_ROOT = os.path.dirname(TYPING_ROOT)
IMAGE_SOURCE = TYPING_ROOT + os.sep + "images" + os.sep + "lessons" + os.sep + "chooser" 

class lessonChooser(gtk.VBox):

    #Initialize the Lesson Chooser    
    def __init__(self, window, vbox, scrolledWin):
        super(lessonChooser, self).__init__(False, 0)
        
        self.win = window
        self.main_vbox = vbox
        self.scroll = scrolledWin
        self.deleter = self.win.connect("delete_event", self.delete_event)

        #Create a list of buttons, so we can hide/show them later.        
        self.buttonList = []

        #Load up all the lessons, so we know what buttons to offer        
        lessons = lesson.Lessons()
        lessons.loadLessonPlan(TYPING_ROOT + os.sep + "lesson.ln")
        lessonList = lessons.getLessonList()

        #Create one button per lesson, show it, and connect it to choosing that lesson        
        for (x) in range(len(lessonList)):
            image = gtk.Image()
            image.set_from_file(IMAGE_SOURCE + os.sep + lessonList[x] + ".gif")
            image.show()
        
            button = gtk.Button()
            button.set_image(image)
            button.show()
            self.add(button)
            self.buttonList.append(button)
            button.connect("clicked", self.chooseLesson, lessonList[x])

        #Create a back button, which will just return us to mode chooser
        button = gtk.Button()
        button.show()
        self.add(button)
        self.buttonList.append(button)
        backimage = gtk.Image()
        backimage.set_from_file(IMAGE_SOURCE + os.sep + "backbutton.gif")
        backimage.show()
        button.set_image(backimage)
        button.connect("clicked", self.back, "Back")

        #Place a lesson game in the window, but
        #keep it hidden until we know what lesson to use.        
        self.lm = lessonmode.LessonGame(window, lessons, True)
        self.main_vbox.add(self.lm)
        self.hide()
        
        self.quit = False
        
    #Called when the window is closed, to make sure we end and
    #Tell the parent it needs to end, too
    def delete_event(self, widget, event, data=None):
        self.quit = True
        gtk.main_quit()
        return False

    #Choose a lesson, hiding all the buttons
    #Start a game for that lesson
    #And then unhide all buttons after the game is over
    def chooseLesson(self, widget, data=None):
        #Cycle through the buttons hiding them
        for x in self.buttonList:
            x.hide()

        #Display the lesson mode stuff        
        self.scroll.hide()
        self.lm.show()

        #Block the close window trigger, so we only call it once        
        self.win.handler_block(self.deleter)

        #Starts the lesson.  Will return true if the lesson ended by the user closing the window
        quit = self.lm.runGame(data)

        #If quit was true, the user closed the window,
        #so call the event we would have called from this level
        #so that we call gtk.main_quit correctly.               
        if(quit):
            self.delete_event(False, False, False)
            return

        #Unblock the trigger, so closing the window once again ends the program        
        self.win.handler_unblock(self.deleter)

        self.scroll.show()

        #Hide the lesson mode objects and reshow all the buttons        
        self.lm.hide()
        for x in self.buttonList:
            x.show()

    #Hides all these buttons, and then quits back up to the mode chooser
    def back(self, widget, data=None):
        self.hide()
        gtk.main_quit()

    #Runs the program, to wait for the a user response
    #self.quit will be true if the window was closed, only.
    #Returns true if the window was closed, false otherwise
    def run(self):
        gtk.main()
        if(not self.quit):
            self.win.disconnect(self.deleter)
        return self.quit